/*!
\file DEdgeData.cpp
\brief Contains implementation of DEdgeData.
\ingroup DOT
*/

#include"DEdgeData.h"

DEdgeData::DEdgeData( QString data ){
	m_id = 0;
	if( !data.contains("[") ){
		_setIDs(data);
	}else{
		QStringList dataParts = data.split( "[" );
		_setIDs( dataParts[0] );
		dataParts = dataParts[1].split( "]" );
		_setOptions(dataParts[0]);
	}
}

DEdgeData::~DEdgeData(){

}

void DEdgeData::_setIDs( QString data ){
	data = data.trimmed();
	QStringList stringList = data.split( QRegExp( "(--)|(->)" ) );
	if( stringList.size() != 2 ){
		return;
	}
	m_from = stringList[0].toInt();
	m_to = stringList[1].toInt();
}

QString DEdgeData::toDOT( bool directed )const{
	QString ret;
	
	ret.append( QString().setNum(m_from) );
	ret.append( directed ? QString( " -> " ) : QString( " -- " ) );
	ret.append( QString().setNum(m_to) );
	ret.append( _dotOptions() );
	ret.append(";");
	
	return ret;
}

void DEdgeData::_setOptions( QString options ){
	QStringList optionParts = options.split( "," );
	for( int i = 0; i < optionParts.size(); i++ ){
		QStringList help = optionParts[i].split("=");
		if( help[0].trimmed() == "color" ){
			help.pop_front();
			m_colourStack.push_front(help.join("=").trimmed());
		}else if( help[0].trimmed() == "label" ){
			m_options.push_back( help[0].trimmed() );
			help.pop_front();
			QString help2 = help.join("=").trimmed();
			help2 = help2.left(help2.size()-1);
			help2 = help2.right(help2.size()-1);
			QStringList help3 = help2.split(":");
			m_id = help3[0].toInt();
			help3.pop_front();
			help2 = help3.join(":").trimmed();
			help2 = help2.replace("\"", "");
			m_values.push_back(help2.prepend("\"").append("\""));
		}else{
			m_options.push_back( help[0].trimmed() );
			help.pop_front();
			m_values.push_back(help.join( "=" ).trimmed());
		} 
	}
}


